﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.ModelInterface;
using QLBH.DataModel;
using QLBH.Model.Controller;
using QLBH.StandardUI;
using QLBH.ModelFactory;

namespace QLBH.SaleProduct
{
    public partial class SearchHDBanHangDlg : Form
    {
        private struct sFindInfo
        {
            public String sophieu { set; get; }            
            public Guid nvid { set; get; }
            public DateTime fromdate { set; get; }
            public DateTime todate { set; get; }
        }

        private sFindInfo finInfo;
        private IHDBanHang model;

        public IHDBanHang Model
        {
            get { return model; }
            set { model = value; }
        }
        public SearchHDBanHangDlg()
        {
            InitializeComponent();

            finInfo = new sFindInfo();
        }

        private void SearchHDBanHangDlg_Load(object sender, EventArgs e)
        {
            //Load list nhan vien
            List<NHANVIEN> users = NhanVienController.GetInstance().GetAll().ToList();
            cmbUser.DataSource = users;
            cmbUser.DisplayMember = "TENNV";

            Guid nvid = AccountHandler.GetCurrentUserLogin().NVID;
            NHANVIEN loginUser = NhanVienController.GetInstance().FindBy(nv => nv.NVID == nvid).FirstOrDefault();

            if (loginUser != null)
            {
                cmbUser.SelectedItem = loginUser;
            }
            // setting for date time to now.

            dtpFromdate.Value = DateTime.Now;
            dtpToDate.Value = DateTime.Now;

            FindResult();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            FindResult();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            // send edit object to parent form and close this dialog
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //show question to assume that user want to delete
            if (ShowMessage.Instance.ShowQuestion("Bạn muốn xóa hóa đơn này?") == DialogResult.Yes)
            {
                try
                {
                    HDBanHangController.GetInstance().Delete(model.ToEntity());
                    FindResult();
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                }
            }
            return;
        }
        private void FindResult()
        {
            List<IHDBanHang> result = new List<IHDBanHang>();
            List<HOADONBANHANG> hdbhs = new List<HOADONBANHANG>();
            if (finInfo.sophieu!=null && finInfo.sophieu!="")
            {
                hdbhs= HDBanHangController.GetInstance().FindBy(
                        hd => hd.SOHD == finInfo.sophieu &&
                        hd.NGUOITAOID == finInfo.nvid &&
                        (hd.NGAYTAO >= finInfo.fromdate &&
                        hd.NGAYTAO <= finInfo.todate)
                    ).ToList();
            }
            else
            {
                hdbhs = HDBanHangController.GetInstance().FindBy( hd=>                     
                      hd.NGUOITAOID == finInfo.nvid &&
                      (hd.NGAYTAO >= finInfo.fromdate &&
                      hd.NGAYTAO <= finInfo.todate)
                  ).ToList();
            }
            
            foreach (HOADONBANHANG item in hdbhs)
            {
                IHDBanHang modelItem = SaleProductModelFactory.GetHDBanHangModelInstance();
                modelItem.InitFromEntity(item);
                result.Add(modelItem);
            }
            dataGridViewEx1.DataSource = new List<IHDBanHang>(result);
        }

        private void dataGridViewEx1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count > 0)
            {
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
                model = dataGridViewEx1.SelectedRows[0].DataBoundItem as IHDBanHang;
            }
            else
            {
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
            }
        }

        private void SearchHDBanHangDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(btnSearch, null);
            }
            else if (e.KeyCode == Keys.Delete)
            {
                btnDelete_Click(btnDelete, null);
            }
            else if (e.KeyCode == Keys.F1)
            {
                tbxSoPhieu.SelectAll();
                tbxSoPhieu.Focus();
            }
            else if (e.KeyCode == Keys.F2)
            {
                btnEdit_Click(btnEdit, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void dataGridViewEx1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count > 0)
            {
                model = dataGridViewEx1.SelectedRows[0].DataBoundItem as IHDBanHang;
            }
            btnEdit_Click(btnEdit, null);
        }

        private void dataGridViewEx1_DataSourceChanged(object sender, EventArgs e)
        {            
        }

        private void cmbUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            finInfo.nvid = (cmbUser.SelectedItem as NHANVIEN).NVID; 
        }

        private void dtpFromdate_ValueChanged(object sender, EventArgs e)
        {
            finInfo.fromdate = dtpFromdate.Value; 
        }

        private void dtpToDate_ValueChanged(object sender, EventArgs e)
        {
            finInfo.todate = dtpToDate.Value; 
        }

        private void tbxSoPhieu_TextChanged(object sender, EventArgs e)
        {
            finInfo.sophieu = tbxSoPhieu.Text;
        }
    }
}
